package iceportal1
import static groovyx.net.http.ContentType.JSON
import java.util.ArrayList 

class UserController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        ArrayList userList = new ArrayList ();
      try {          
          withRest(uri: "http://localhost:8080/iceapi/services/") {
              def response = get(path:"office/"+session.officeid+"/users",
                              headers:['Authorization':session.authKey],
                              requestContentType: JSON, 
                              contentType: JSON)
               
               response.data.users.entries.each {
                 User user = new User ()
                 user.userName = it.userName 
                 userList.add (user)                  
               }           
               
          }          
        } catch (e) {
        }
        [userInstanceList: userList, userInstanceTotal: userList.size()]
    }

    def create = {
        
    }

    def save = {
        def userInstance = new User(params)
      try {          
          withRest(uri: "http://localhost:8080/iceapi/services/") {
              post(path:"user",
                  headers:['Authorization':session.authKey],
                  requestContentType: JSON, 
                  contentType: JSON,
                  body: ['userName': ['$': params.userName], 'domain': ['$': params.domain], 'officeName': ['$': session.officeid], 'password': ['$': params.password]])
          }          
          redirect(action: "list")
        } catch (e) {
          e.printStackTrace();
          userInstance.errors.rejectValue ("userName","Create failed") 
          render(view: "create", model: [userInstance: userInstance])
        }
    }

    def show = {
        def userInstance = new User (params)
      try {          
          withRest(uri: "http://localhost:8080/iceapi/services/") {
              def response = get(path:"user/"+params.id,
                              headers:['Authorization':session.authKey],
                              requestContentType: JSON, 
                              contentType: JSON)
               
               userInstance.userName = response.data.userName  
               userInstance.domain = response.data.domain
               
          }          
        } catch (e) {
        }
        if (!userInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])}"
            redirect(action: "list")
        }
        else {
            [userInstance: userInstance]
        }
    }

    def edit = {
        def userInstance = new User (params)
      try {          
          withRest(uri: "http://localhost:8080/iceapi/services/") {
              def response = get(path:"user/"+params.id,
                              headers:['Authorization':session.authKey],
                              requestContentType: JSON, 
                              contentType: JSON)
               
               userInstance.userName = response.data.userName  
               userInstance.domain = response.data.domain
          }          
        } catch (e) {
        }
        if (!userInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [userInstance: userInstance]
        }
    }

    def update = {
        def userInstance = User.get(params.id)
        if (userInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (userInstance.version > version) {
                    
                    userInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'user.label', default: 'User')] as Object[], "Another user has updated this User while you were editing")
                    render(view: "edit", model: [userInstance: userInstance])
                    return
                }
            }
            userInstance.properties = params
            if (!userInstance.hasErrors() && userInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'user.label', default: 'User'), userInstance.id])}"
                redirect(action: "show", id: userInstance.id)
            }
            else {
                render(view: "edit", model: [userInstance: userInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def userInstance = User.get(params.id)
        if (userInstance) {
            try {
                userInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'user.label', default: 'User'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'user.label', default: 'User'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])}"
            redirect(action: "list")
        }
    }
}
